function [final_list] = order_list(list, genes)

% ----------------------------------------------
% This function is used to put an adjacency list
% in a given order.  It also replaces strings
% with numerical references to the given list.
%
% USAGE:
% ordered_list = order_list(list, genes)
% ----------------------------------------------

list_labels = {};

for i = 1:length(list)
    list_labels{end+1} = list{i}{1};
end

ordered_list = {};

for i = 1:length(genes)
    mylist = {};
    [t loc] = ismember(genes{i}, list_labels);
    if t
        mylist = list{loc};
    else
        mylist{end+1} = genes{i};
    end
    ordered_list{end+1} = mylist;
end

final_list = {};

for i = 1:length(ordered_list)
    mylist = ordered_list{i};
    numbered_list = [];
    for j = 1:length(mylist)
        [t loc] = ismember(mylist{j}, genes);
        if ~t
            error('Subnetworks not created properly.');
        else
            numbered_list = [numbered_list loc];
        end
    end
    final_list{end+1} = numbered_list;
end